# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql

class Tenxun1Pipeline:
    def process_item(self, item, spider):
        return item

class mysqlPipeline:
    def __init__(self,host,user,password,database,port):
        self.host=host
        self.user=user
        self.password=password
        self.database=database
        self.port=port

    @classmethod
    def from_crawler(cls,crawler):
        return cls(
            host=crawler.settings.get("MYSQL_HOST"),
            user=crawler.settings.get("MYSQL_USE"),
            password=crawler.settings.get("MYSQL_PASSWORD"),
            database=crawler.settings.get("MYSQL_DATABASE"),
            port=crawler.settings.get("MYSQL_PORT")
        )

    def process_item(self, item, spider):
        sql="insert into hr(pid,pName,city,bgName,cname,responsibility,requirment) values('{}','{}','{}','{}','{}','{}','{}')".format(item['PostId'],item['RecruitPostName'],item['LocationName'],item['BGName'],item['CategoryName'],item['Responsibility'],item['Requirement'])

        print(sql)
        self.cursor.execute(sql)
        self.db.commit()
        return item
    def open_spider(self,spider):
        self.db=pymysql.connect(host=self.host,user=self.user,password=self.password,database=self.database,port=self.port,charset='utf8')
        self.cursor=self.db.cursor()

    def close_spider(self,spider):
        self.db.close()

